package com.xjt.query.action;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.opensymphony.xwork2.Action;
import com.xjt.common.StringUtils;
import com.xjt.common.Types.USER_AUTHORITY;
import com.xjt.query.dao.interf.QueryUserDao;
import com.xjt.query.dao.pojo.QueryUser;
import com.xjt.security.MD5;

public class AccountAction extends BaseAction {

    private static final long serialVersionUID = -8968794125853342962L;

    private QueryUserDao queryUserDao;

    private QueryUser userInfo;

    private String oldPwd;

    private String userConfirmPwd;

    private Map<String, String> errorMap = new LinkedHashMap<String, String>();

    private String alertMsg;

    /**
     * 显示注册页面
     * 
     * @return
     */
    public String showRegister() {
        return Action.SUCCESS;
    }

    /**
     * 注册
     * 
     * @return
     */
    public String doRegister() {
        if (null == userInfo) {
            return Action.INPUT;
        }
        // 校验输入
        checkInput(true);
        if (MapUtils.isNotEmpty(errorMap)) {
            return Action.INPUT;
        }
        QueryUser existUser = queryUserDao.selectUserByUserId(userInfo.getUserId().trim());
        if(null != existUser){
            errorMap.put("userId", "用户名已存在！");
            return Action.INPUT;
        }
        userInfo.setUserPwd(MD5.str2md5(userInfo.getUserPwd()));
        userInfo.setUserAuthority(USER_AUTHORITY.FREE);
        queryUserDao.insertUser(userInfo);
        alertMsg = "注册成功!";
        return Action.SUCCESS;
    }

    public String showAccount() {
        userInfo = this.getLoginUser();
        return Action.SUCCESS;
    }

    /**
     * 修改帐号
     * 
     * @return
     */
    public String editAccount() {
        if (null == userInfo) {
            return Action.INPUT;
        }
        // 校验输入
        checkInput(false);
        if (MapUtils.isNotEmpty(errorMap)) {
            return Action.INPUT;
        }
        queryUserDao.updateUserById(userInfo, this.getLoginUser().getId());
        alertMsg = "修改成功!";
        return Action.SUCCESS;
    }

    /**
     * 显示修改密码页面
     */
    public String showEditPassword() {
        return Action.SUCCESS;
    }

    /**
     * 修改密码
     */
    public String editPassword() {
        if (null == userInfo) {
            return Action.INPUT;
        }
        if (StringUtils.isBlank(oldPwd)) {
            errorMap.put("oldPwd", "旧密码不能为空！");
        }
        if (StringUtils.isBlank(userInfo.getUserPwd())) {
            errorMap.put("userPwd", "密码不能为空！");
        }
        if (StringUtils.isNotBlank(userInfo.getUserPwd()) && !userInfo.getUserPwd().equals(userConfirmPwd)) {
            errorMap.put("userConfirmPwd", "密码和重复密码不一致！");
        }
        String oldPassword = MD5.str2md5(oldPwd);
        if (!oldPassword.equals(this.getLoginUser().getUserPwd())) {
            errorMap.put("oldPwd", "旧密码不正确！");
        }
        if (MapUtils.isNotEmpty(errorMap)) {
            return Action.INPUT;
        }
        QueryUser updateInfo = new QueryUser();
        // 密码加密
        updateInfo.setUserPwd(MD5.str2md5(this.userInfo.getUserPwd()));
        queryUserDao.updateUserById(updateInfo, this.getLoginUser().getId());
        alertMsg = "修改成功!";
        return Action.SUCCESS;
    }

    /**
     * 校验输入
     * 
     * @param isNew 是否新注册（是的话需要校验密码）
     */
    private void checkInput(boolean isNew) {
        if (StringUtils.isBlank(userInfo.getUserId())) {
            errorMap.put("userId", "用户名不能为空！");
        }
        if (isNew) {
            if (StringUtils.isBlank(userInfo.getUserPwd())) {
                errorMap.put("userPwd", "密码不能为空！");
            }
            if (StringUtils.isNotBlank(userInfo.getUserPwd()) && !userInfo.getUserPwd().equals(userConfirmPwd)) {
                errorMap.put("userConfirmPwd", "密码和重复密码不一致！");
            }
        }
        if (StringUtils.isBlank(userInfo.getUserName())) {
            errorMap.put("userName", "姓名不能为空！");
        }
        if (StringUtils.isBlank(userInfo.getMobileNo())) {
            errorMap.put("mobileNo", "手机号不能为空！");
        }
        if (StringUtils.isBlank(userInfo.getProvince()) || StringUtils.isBlank(userInfo.getCity())) {
            errorMap.put("province", "所属地区不能为空！");
        }
        if (StringUtils.isBlank(userInfo.getEmail())) {
            errorMap.put("email", "E-mail不能为空！");
        }
        if (StringUtils.isBlank(userInfo.getQq())) {
            errorMap.put("qq", "QQ不能为空！");
        }
    }

    /**
     * 显示支付方式页面
     * 
     * @return
     */
    public String showPayType() {
        return Action.SUCCESS;
    }

    /**
     * @return the userInfo
     */
    public QueryUser getUserInfo() {
        return userInfo;
    }

    /**
     * @param userInfo the userInfo to set
     */
    public void setUserInfo(QueryUser userInfo) {
        this.userInfo = userInfo;
    }

    /**
     * @param queryUserDao the queryUserDao to set
     */
    public void setQueryUserDao(QueryUserDao queryUserDao) {
        this.queryUserDao = queryUserDao;
    }

    /**
     * @return the errorMap
     */
    public Map<String, String> getErrorMap() {
        return errorMap;
    }

    /**
     * @param errorMap the errorMap to set
     */
    public void setErrorMap(Map<String, String> errorMap) {
        this.errorMap = errorMap;
    }

    /**
     * @return the alertMsg
     */
    public String getAlertMsg() {
        return alertMsg;
    }

    /**
     * @param alertMsg the alertMsg to set
     */
    public void setAlertMsg(String alertMsg) {
        this.alertMsg = alertMsg;
    }

    /**
     * @return the oldPwd
     */
    public String getOldPwd() {
        return oldPwd;
    }

    /**
     * @param oldPwd the oldPwd to set
     */
    public void setOldPwd(String oldPwd) {
        this.oldPwd = oldPwd;
    }

    /**
     * @return the userConfirmPwd
     */
    public String getUserConfirmPwd() {
        return userConfirmPwd;
    }

    /**
     * @param userConfirmPwd the userConfirmPwd to set
     */
    public void setUserConfirmPwd(String userConfirmPwd) {
        this.userConfirmPwd = userConfirmPwd;
    }

}
